#ifndef LCD_H
#define LCD_H

#include "def.h"

#define LOW21BIT(n) ((n) & 0x1fffff)

#define LCD_TYPE "LCD43" /* 4.3 inch */

#define LCD_WIDTH 480
#define LCD_HEIGHT 272

/* LCDCON1 setting */
#define CLKVAL 4 /* [17:8] TFT: VCLK = HCLK / [(CLKVAL+1) x 2] = 10M ( CLKVAL >= 0 ) HCLK=100M VCLK=9 */
#define MMODE 0 /* [7] 0 = Each Frame 1 = The rate defined by the MVAL */
#define PNRMODE 3 /* [6:5] 11 = TFT LCD panel */
#define BPPMODE 12 /* [4:1] 1101 = 24 bpp for TFT */
#define ENVID 0 /* [0] 0 = Disable 1 = Enable the video output and the LCD control signal. */

/* LCDCON2 setting */
#define VBPD 2 /* [31:24] */
#define LINEVAL (LCD_HEIGHT - 1) /* [23:14] */
#define VFPD 4 /* [13:6] */
#define VSPW 8 /* [5:0] */

/* LCDCON3 setting */
#define HBPD 11 /* [25:19] */
#define HOZVAL (LCD_WIDTH - 1) /* [18:8] */
#define HFPD 19 /* [7:0] */

/* LCDCON4 setting */
#define MVAL 13 /* [15:8] */
#define HSPW 30 /* [7:0] */

/* LCDCON5 setting */
#define BPP24BL 0 /* [12] hight 8 bit disable */
#define FRM565 1 /* [11] */
#define INVVCLK 0 /* [10] fall */
#define INVVLINE 1 /* [9] */
#define INVVFRAME 1 /* [8] */
#define INVVD 0 /* [7] */
#define INVVDEN 0 /* [6] */
#define INVPWREN 0 /* [5] */
#define INVLEND 1 /* [4] */
#define PWREN 1 /* [3] */
#define ENLEND 1/* [2] */
#define BSWP 0 /* [1] */
#define HWSWP 1 /* [0] */

/* LCDSADDR1 setting */
#define LCDBANK  ((u32)lcd_buffer >> 22) /* [29:21] */
#define LCDBASEU LOW21BIT(((u32)lcd_buffer ) >> 1) /* [20:0] */

/* LCDSADDR2 setting */
#define LCDBASEL (LCDBASEU + (PAGEWIDTH + OFFSIZE) * (LINEVAL + 1) * 2) /* [20:0] */

/* LCDSADDR3 setting */
#define OFFSIZE 0 /* [21:11] phy=virtual */
#define PAGEWIDTH LCD_WIDTH /* [10:0] */

#define WHITE ((0x1f << 11) | (0x3f << 5) | (0x1f << 0))
#define RED ((0x1f << 11) | (0x00 << 5) | (0x00 << 0))
#define GREEN ((0x00 << 11) | (0x3f << 5) | (0x00 << 0))
#define BLUE ((0x00 << 11) | (0x00 << 5) | (0x1f << 0))

void lcd_init(void);
void lcd_set_pixel(u32 x, u32 y, color c);
void lcd_fill_backgroud(color c);
void lcd_draw_hline(u32 x0, u32 x1, u32 y, color c);
void lcd_draw_vline(u32 y0, u32 y1, u32 x, color c);
void lcd_draw_rect(u32 x0, u32 y0, u32 x1, u32 y1, color c);
    
#endif
